#include "Random.h"

CRandom::CRandom(void): iMin(0), iMax(0), iMod(0), iRep(0)
{
	iRandGen = new TrueRand::GenRand();
}

CRandom::~CRandom(void)
{
	iGenNumbers.clear();
	delete iRandGen;
}

void CRandom::SetRepetition(unsigned int aMaxRepetitions)
{
	iRep = aMaxRepetitions;
}

void CRandom::SetMin(unsigned int aMin)
{
	iMin = aMin;
}

void CRandom::SetMax(unsigned int aMax)
{
	iMax = aMax;
	if(iMax > iMin)
	{
		if(iRep != 0)
		{
			iGenNumbers.clear();
			for(unsigned int i = iMin; i<iMax+1; i++)
				iGenNumbers.push_back(i);
		}
		iMod = iMax - iMin + 1;
	}
	else
		iMod = 0;
}

unsigned int CRandom::Min()
{
	return iMin;
}

unsigned int CRandom::Max()
{
	return iMax;
}

unsigned int CRandom::Next()
{
	if(iRep != 0)
	{
		unsigned int mod = iGenNumbers.size();
		if(mod == 0)
			return 0;

		unsigned int index = iRandGen->GetRandInt() % mod; 
		std::list<unsigned int>::iterator it = iGenNumbers.begin();
		std::advance(it,index);
		unsigned int retVal = *it;
		iGenNumbers.erase(it);
		return retVal;
	}
	else
	{
		if(iMod == 0)
			return 0;

		unsigned int randVal = iRandGen->GetRandInt();

		return (randVal % iMod) + iMin;
	}
}

int CRandom::RemainingNumbers()
{
	if(iRep != 0)
		return iGenNumbers.size();
}